output: html_document: pdf_document: default word_document: default title: “Assignment 8: Visualization - Making a bar race” —
How to do it?:
Open the Rmarkdown file of this assignment (link) in Rstudio.
Right under each question, insert a code chunk
(you can use the hotkey Ctrl + Alt + I to add a code chunk)
and code the solution for the question.
Knit the rmarkdown file (hotkey:
Ctrl + Alt + K) to export an html.
Publish the html file to your Githiub Page.
Submission: Submit the link on Github of the assignment to Canvas
gganimate and gifski
then restart Rstudio. Using the Adult Census Income data,
make an animation using geom_point and
transition_states.#install.packages('gifski')
library(gganimate)
library(gifski)
library(ggplot2)
library(tidyverse)
library(knitr)
df <- read_csv('https://bryantstats.github.io/math421/data/adult_census.csv')
g <- ggplot(df,
aes(x = capital.loss,
y = hours.per.week))+
geom_point(size=4) +
transition_states(income)+
labs(title = 'income: {closest_state}')
animate(g)
Adult Census Income data, make an animation
using geom_bar and transition_states.df %>% ggplot(aes(x = income,
fill=sex))+
geom_bar(position = 'fill')+
transition_states(education) +
labs(title = 'education: {closest_state}')
df <- read_csv('https://covid19.who.int/WHO-COVID-19-global-data.csv')
library(lubridate)
df$Country <- str_replace(df$Country, "United Kingdom of Great Britain and Northern Ireland", "UK and NI")
df$months <- month(df$Date_reported)
d1 <- df %>% group_by(months, Country) %>% summarise(mean = mean(Cumulative_deaths))
d2 <- d1 %>% group_by(months) %>% mutate(rank=rank(-mean))
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country)) + geom_col()+
geom_text(aes(y = mean, label = Country), hjust = 1.4)+
coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
labs(title = 'month {closest_state}', x='', y='Cumulative Deaths', fill='Country')+
theme(plot.title = element_text(hjust = 1, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
transition_states(months)+
ease_aes("cubic-in-out")
animate(a1, nframes = 400)
df <- read_csv('https://covidtracking.com/data/download/all-states-history.csv')
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
df <- df %>% filter(deathIncrease>0, positiveIncrease>0)
library(lubridate)
df$months <- months(df$date)
d1 <- df %>% group_by(months, state) %>% summarise(mean = mean(death))
d2 <- d1 %>% group_by(months) %>% mutate(rank=rank(-mean))
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=state, fill=state, label=state)) + geom_col()+
geom_text(aes(y = mean, label = state), hjust = 1.4)+
coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
labs(title = 'month {closest_state}', x='', y='Total Number of Deaths', fill='state')+
theme(plot.title = element_text(hjust = 1, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
transition_states(months)+
ease_aes("cubic-in-out")
animate(a1, nframes = 400)